﻿using log4net;
using Mangox.Common;
using Mangox.Model.MyBusinessDB;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;

namespace Mangox.BLL.Applet
{
    public class CommentBLL
    {
        protected static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        private static string className = (new StackTrace()).GetFrame(1).GetMethod().ReflectedType.Name + "/";


        /// <summary>
        /// 添加评论
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool AddComment(T_Comment model)
        {
            try
            {
                log.InfoFormat("添加评论信息：" + JsonCommon.ToJson<T_Comment>(model));

                if (model == null || model.TopicId <= 0)
                {
                    return false;   
                }

                using (MyBusinessDBContext db = new MyBusinessDBContext())
                {
                    var temp = new T_Comment()
                    {
                        TopicId = model.TopicId,
                        TopicType = model.TopicType,
                        FromUid = model.FromUid,
                        FromOpenid = model.FromOpenid,
                        ContentMsg = model.ContentMsg,
                        State = model.State = 1,
                        CreateTime = DateTime.Now
                    };

                    db.T_Comment.Add(temp);
                    return db.SaveChanges() > 0;
                }
            }
            catch (Exception ex)
            {
                log.ErrorFormat(className + "AddComment fail-{0}", ex);
            }
            return false;
        }


        public bool DelComment(T_Comment model)
        {
            try
            {
                using (MyBusinessDBContext db = new MyBusinessDBContext())
                {
                    db.T_Comment.Remove(model);
                    return db.SaveChanges() > 0;
                }
            }
            catch (Exception ex)
            {
                log.ErrorFormat(className + "DelComment fail-{0}", ex);
            }
            return false;
        }



    }
}
